﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Maticsoft.Common;
using Newtonsoft.Json;
using System.Data;

namespace jmwqz.ajax
{
    /// <summary>
    /// good 的摘要说明
    /// </summary>
    public class good : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string _action = MyCommFun.QueryString("myact");
            if (_action == "getlist")
            {
                Maticsoft.BLL.tb_item bitem = new Maticsoft.BLL.tb_item();
                List<Maticsoft.Model.tb_item> items = bitem.GetModelList(" is_sale = 1 and expire_date > '" + DateTime.Now.ToString("yyyy-MM-dd") + "' ");
                if (items == null || items.Count == 0)
                {
                    context.Response.Write("1");
                }
                else
                {
                    context.Response.Write(JsonConvert.SerializeObject(items));
                }
            }
            else if (_action == "duihuan")
            {
                try
                {
                    //if (!Maticsoft.Common.Util2.IsMobile)
                    //{
                    //    context.Response.Write("1");
                    //    return;
                    //}
                    if (context.Session["userid"] == null)
                    {
                        context.Response.Write("1");
                        return;
                    }
                    
                    int userid = Convert.ToInt32(context.Session["userid"].ToString());
                    Maticsoft.BLL.tb_userdata buser = new Maticsoft.BLL.tb_userdata();
                    Maticsoft.Model.tb_userdata muser = buser.GetModel(userid);
                    if (muser == null)
                    {
                        context.Response.Write("1");
                        return;
                    }
                    int itemid = MyCommFun.RequestInt("id");
                    Maticsoft.BLL.tb_item bitem = new Maticsoft.BLL.tb_item();
                    Maticsoft.Model.tb_item item = bitem.GetModel(itemid);
                    if (item == null)
                    {
                        context.Response.Write("1");
                        return;
                    }
                    if (muser.freezejifen > 3000)
                    {
                        if (muser.jifen < item.price)
                        {
                            context.Response.Write("1");
                            return;
                        }
                    }
                    else
                    {
                        muser.jifen += muser.freezejifen;
                        muser.freezejifen = 0;
                        if (muser.jifen < item.price)
                        {
                            context.Response.Write("1");
                            return;
                        }
                    }

                    Maticsoft.BLL.wx_shop_user_addr baddr = new Maticsoft.BLL.wx_shop_user_addr();
                    DataSet ds = baddr.GetList(" wid=" + userid);
                    Maticsoft.Model.wx_shop_user_addr madder = null;
                    if (Maticsoft.Common.Util2.CheckDataSet(ds))
                    {
                        List<Maticsoft.Model.wx_shop_user_addr> list = baddr.DataTableToList(ds.Tables[0]);
                        if (list != null && list.Count > 0)
                        {
                            madder = list[0];
                        }
                    }
                    //if (madder == null)
                    //{
                    //    context.Response.Write("2");
                    //    return;
                    //}

                    Maticsoft.BLL.tb_duihuan btx = new Maticsoft.BLL.tb_duihuan();
                    Maticsoft.Model.tb_duihuan mtx = new Maticsoft.Model.tb_duihuan();
                    mtx.inquiretime = DateTime.Now;
                    mtx.uid = userid;
                    mtx.itemid = itemid;
                    mtx.state = "0";
                    if (btx.Add(mtx))
                    {
                        muser.jifen -= item.price;
                        buser.Update(muser);
                        if (item.id == 18)
                        {
                            item.stock -= 2;
                        }
                        else
                        {
                            item.stock -= 1;
                        }
                        if (item.stock <= 0)
                        {
                            item.stock = 0;
                        }
                        bitem.Update(item);
                        context.Response.Write("0");
                        return;
                    }
                }
                catch (System.Exception ex)
                {
                    context.Response.Write("1");
                    return;
                }
                context.Response.Write("1");
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}